package com.heaven.controller;

import com.heaven.pojo.Result;
import com.heaven.pojo.User;
import com.heaven.service.UserService;
import com.heaven.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@CrossOrigin(origins="http://localhost:7000")
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private UserService userService;
    @PostMapping
    public Result login(@RequestBody User user){
        log.info("员工登录:{}",user);
        User e = userService.Login(user);

        if(e != null){
            Map<String, Object> claims = new HashMap<>();
            claims.put("id", e.getId());
            claims.put("name", e.getName());
            claims.put("username", e.getUsername());
            claims.put("gender", e.getGender());
            claims.put("image", e.getImage());
            claims.put("province", e.getProvince());
            claims.put("area", e.getArea());


            String jwt = JwtUtils.generateJwt(claims);//jwt包含了当前登录的员工信息
            return Result.success(jwt);
        }

        return Result.error("用户名或密码错误");
    }
}
